package ui_window;

import config.FrameConfig;
import config.GameConfig;
import config.LayerConfig;
import control.GameControl;
import control.PlayerControl;
import dto.GameDto;
import ui.Img;
import ui.Layer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 何惠民
 */
public class JPanelGame extends JPanel {

    private static final int BTN_SIZE_W = GameConfig.getFrameConfig().getButtonConfig().getButtonW();

    private static final int BTN_SIZE_H = GameConfig.getFrameConfig().getButtonConfig().getButtonH();

    private List<Layer> layers = null;

    private JButton btnStart;

    private JButton btnConfig;

    private JButton nandu;

    private JButton nandu2;

    public GameDto dto;




    private GameControl gameControl = null;

    public JPanelGame(GameControl gameControl, GameDto dto){
        this.dto=dto;
        //连接游戏控制器
        this.gameControl = gameControl;
        //设置布局管理器为
        this.setLayout(null);
        //初始化组件
        this.initComponent();
        //层初始化
        this.initLayer(dto);
        //安装键盘监听器
        this.addKeyListener(new PlayerControl(gameControl));
    }

    /**
     * 初始化组件
     */
    private void initComponent(){
        //创建难度按钮1
        this.nandu = new JButton("难度1");
        //按钮位置
        this.nandu.setBounds(830,550,100,50);
        // 给难度按钮添加事件监听
        this.nandu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dto.gameMap=new boolean[10][18];
                gameControl.randomlyProduceMap(dto.flag2,13,5,19);
                requestFocus();
            }
        });
        //创建难度按钮2
        this.nandu2 = new JButton("难度2");
        //按钮位置
        this.nandu2.setBounds(980,550,100,50);
        // 给难度按钮添加事件监听
        this.nandu2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dto.gameMap=new boolean[10][18];
                gameControl.randomlyProduceMap(dto.flag2,10,8,19);
                requestFocus();
            }
        });
        //难度按钮添加到主面板
        this.add(nandu);
        this.add(nandu2);

        //初始化开始按钮
        this.btnStart = new JButton(Img.BTN_START);
        //初始化开始按钮位置
        this.btnStart.setBounds(
                GameConfig.getFrameConfig().getButtonConfig().getStartX(),
                GameConfig.getFrameConfig().getButtonConfig().getStartY(),
                BTN_SIZE_W,BTN_SIZE_H);
        // 给开始按钮添加事件监听
        this.btnStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameControl.start();
                requestFocus();
            }
        });
        //添加到按钮面板
        this.add(btnStart);
        //初始化设置按钮
        this.btnConfig = new JButton(Img.BTN_CONFIG);
        //初始化设置按钮位置
        this.btnConfig.setBounds(
                GameConfig.getFrameConfig().getButtonConfig().getUserConfigX(),
                GameConfig.getFrameConfig().getButtonConfig().getUserConfigY(),
                BTN_SIZE_W,BTN_SIZE_H);
        //给设置按钮添加事件监听
        this.btnConfig.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameControl.showUserConfig();
            }
        });
        //添加到按钮面板
        this.add(btnConfig);
    }
    /**
     * 层初始化
     */
    private void initLayer(GameDto dto){
        try {
            //获取游戏配置
            FrameConfig fCfg = GameConfig.getFrameConfig();
            //获得层配置
            List<LayerConfig> layersCfg = fCfg.getLayersConfig();
            //创建游戏层数组
            layers=new ArrayList<Layer>(layersCfg.size());
            //循环创建所有层对象
            for (LayerConfig layerCfg : layersCfg){
                //获得类对象
                Class<?> cls = Class.forName(layerCfg.getClassName());
                //获得构造函数
                Constructor <?> ctr = cls.getConstructor(int.class,int.class,int.class,int.class);
                //调用构造函数创建类对象
                Layer layer=(Layer) ctr.newInstance(
                        layerCfg.getX(),layerCfg.getY(),layerCfg.getW(),layerCfg.getH()
                );
                //设置游戏数据对象
                layer.setDto(dto);
                //把创建的Layer对象放入集合
                layers.add(layer);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override    //覆盖父类方法
    public void paintComponent(Graphics g){
        //调用基类方法
        super.paintComponent(g);
        //绘制游戏界面
        for (int i = 0; i< layers.size(); layers.get(i++).paint(g)){ }
    }
    /**
     * 控制按钮是否可点击
     */
    public void buttonSwitch(boolean onOff){
        this.btnConfig.setEnabled(onOff);
        this.btnStart.setEnabled(onOff);
        this.nandu.setEnabled(onOff);
        this.nandu2.setEnabled(onOff);
    }


}
